<template>
  <Container class="brand-list">
    <Tabs v-model="tab">
      <Button
        size="large"
        class="item"
        type="primary"
        slot="extra"
        icon="md-add"
        @click="onAddType"
      >新增政策</Button>
      <TabPane label="串货例外政策设置" name="tab1">
        <Table
          :height="options.tableHeight"
          stripe
          :columns="list.columns"
          :data="list.data"
          no-data-text="亲，没有找到相关记录哦！~"
        ></Table>
        <div class="pager">
          <Page
            :total="list.totalItem"
            :current="list.currentPage"
            :page-size="list.pageSize"
            @on-change="onPageChange"
            @on-page-size-change="onPageSizeChange"
            show-sizer
            placement="top"
          ></Page>
        </div>
        <Loader :status="list.status" @refresh="getData"></Loader>
      </TabPane>
    </Tabs>
    <BunchEditItem ref="storeEdit" @complete="getData"></BunchEditItem>
  </Container>
</template>
<script>
import table from "@/table";
import BunchListData from "./list/bunchListData";
import BunchEditItem from "./edit/BunchEditItem";
import { clearEmpty } from "@/util";
export default {
  components: {
    BunchEditItem
  },
  data() {
    return {
      keyword: "",
      tab: "tab1",
      list: BunchListData,
      options: {
        tableHeight: 300
      }
    };
  },
  mounted() {
    this.list.currentPage = ~~this.$route.query.currentPage || 1;
    this.list.pageSize = ~~this.$route.query.pageSize || this.list.pageSize;
    this.initTable();
    this.$bus.$on("onEdit", this.onEdit);
    this.$bus.$on("onDelete", this.onDelete);
    this.getData();
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    ...table,
    onSearchBtnClick() {
      this.list.currentPage = 1;
      this.getData();
    },
    getData() {
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        policyName: this.keyword
      };
      param = clearEmpty(param);
      this.list.status = "loading";
      this.$ajaxGet("/setting/scancode_special_policy/page", param)
        .then(data => {
          this.list.data = [];
          this.list.totalItem = Number(data.total);
          this.list.data = (data.records || []).map(item => {
            return {
              name: item.policyName,
              createDate: item.createDate,
              id: item.id
            };
          });
          this.list.status = "pass";
          this.backTableTop();
        })
        .catch(() => {
          this.list.status = "error";
        });
    },
    onAddType() {
      this.$router.push({
        path: "/policy/bunch/edit"
      });
    },
    onEdit(id) {
      this.$router.push({
        path: "/policy/bunch/edit",
        query: { id: id }
      });
    },
    onDelete(id, name, index) {
      this.$Modal.confirm({
        title: "删除",
        content: "确定删除当前政策？",
        onOk: () => {
          this.$ajaxPost(`/setting/scancode_special_policy/delete/${id}`).then(
            data => {
              this.$Notice.success({
                title: "删除成功",
                desc: `<span class="font-red">${name}</span> 已被删除。`
              });
              this.getData();
            }
          );
        }
      });
    },
    onPageChange(number) {
      this.list.currentPage = number;
      this.$router.push({
        name: "policy/bunch/list",
        query: {
          currentPage: this.list.currentPage,
          pageSize: this.list.pageSize
        }
      });
      this.getData();
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1;
      this.list.pageSize = pageSize;
      this.$router.push({
        name: "policy/bunch/list",
        query: {
          currentPage: this.list.currentPage,
          pageSize: this.list.pageSize
        }
      });
      this.getData();
    }
  },
  watch: {
    $route() {
      let page = ~~this.$route.query.currentPage;
      let table = this.list;
      if (table.currentPage !== page) {
        table.currentPage =
          ~~this.$route.query.currentPage === 0
            ? 1
            : ~~this.$route.query.currentPage;
        this.getData();
      }
    }
  }
};
</script>
